package com.ncloud.aspect;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.util.HttpUtils;
import com.ncloud.util.MacUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * Created by shiqiang on 2017/4/3.
 */
@Aspect
@Component
public class HttpAspect {
    private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);

    @Pointcut("execution(public * com.ncloud.web.OpenNCloudWeb.*(..)) or execution(public * com.ncloud.web.FileUploadController.*(..)) " +
            "or execution(public * com.ncloud.web.OpenNCloudOutController.*(..))")
    public void log(){

    }
    @Before("log()")
    public void beforeDo(JoinPoint joinPoint){
        logger.info("记录日志开始");
        JSONObject log = new JSONObject();
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        Object userID = request.getAttribute("userID");
        log.put("userId",userID);
        //url
        log.put("url",request.getRequestURL());
        //logger.info("url={}", request.getRequestURL());
        //method
        log.put("method",request.getMethod());
        log.put("query",request.getQueryString());
        log.put("pathInfo",request.getPathInfo());
        //logger.info("method={}", request.getMethod());
        //ip
        //logger.info("ip={}", HttpUtils.getIpAddr(request));
        log.put("ip",HttpUtils.getIpAddr(request));
        //logger.info("host={}", request.getRemoteHost());
        log.put("host",request.getRemoteHost());
        //mac
        //logger.info("mac={}", MacUtils.getMac());
       // log.put("mac",MacUtils.getMac());
        //logger.info("userAgent={}", HttpUtils.getUserAgent(request));
        log.put("userAgent",HttpUtils.getUserAgent(request));
        //logger.info("refer={}", HttpUtils.getRefer(request));
        log.put("refer",HttpUtils.getRefer(request));
        //className
       // logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
        log.put("class_method",joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
        //classmethod
       // logger.info("class_param={}", joinPoint.getArgs());
        log.put("class_param", Arrays.toString(joinPoint.getArgs()));
        logger.info("log{}",log.toJSONString());
    }

   /* @After("log()")
    public void afterDo(){
        logger.info("记录日志结束！");
    }*/
    @AfterReturning(returning = "object",pointcut = "log()")
    public void afterReturnning(Object object){
        logger.info("response={}", object.toString());
    }
}
